package com.berchen.busidb.service;

import com.alibaba.fastjson.JSONObject;
import com.berchen.busidb.dao.UserMapper;
import com.berchen.common.KeyEnum;
import com.berchen.common.TokenModel;
import com.berchen.common.busidb.model.UserVo;
import com.berchen.common.busidb.service.UserService;
import com.berchen.utils.RedisUtils;
import com.berchen.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.TimeUnit;

@Service
@Transactional(transactionManager = "busiTx",propagation = Propagation.REQUIRED)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisUtils redisUtils;
    @Override
    public UserVo loginPhone(String phone, String code) {

        // 查询数据库该号码是否注册
        UserVo vo=new UserVo();
        vo.setPhone(phone);
        UserVo dbVo=userMapper.getUserInfoByCondition(vo);

        if(dbVo==null){
            // 插入数据库注册
            userMapper.insertUser(vo);
            dbVo=vo;
        }
        // 生成token
        String token = createToken(phone,dbVo.getId());
        redisUtils.set(KeyEnum.USER_TOKEN.getKey()+token, dbVo.getId().toString(),30, TimeUnit.DAYS);
        dbVo.setToken(token);
        return dbVo;
    }

    @Override
    public UserVo loginAccount(String account, String password) {
        // 查询数据库数据 根据账号 然后用密码进行匹配
        UserVo vo=new UserVo();
        vo.setAccount(account);
        UserVo dbVo=userMapper.getUserInfoByCondition(vo);
        if(dbVo==null){
            return vo; // 账号不存在
        }
        // 比较密码是否匹配
        if(password.equals(dbVo.getPassword())){
            // 生成token
            String token=createToken(dbVo.getPhone(),dbVo.getId());
            redisUtils.set(KeyEnum.USER_TOKEN.getKey()+token,dbVo.getId()+"",30, TimeUnit.DAYS);
            dbVo.setToken(token);
            return dbVo;
        }
        return null;
    }
    private String createToken(String phone,Long id){
        TokenModel tokenModel=new TokenModel();
        tokenModel.setId(id);
        tokenModel.setPhone(phone);
        tokenModel.setTimestmp(System.currentTimeMillis()+"");
        return  TokenUtils.createToken(tokenModel);
    }

    /**
     * @CachePut注解的作用：1、进行方法调用，2、将方法结果更新到缓存中   该方法返回值为void 所以不会更新 需要更新方法
     * @param vo
     */
    @Override
//    @CachePut(cacheNames = "getUserInfo",key = "#vo.id",unless = "#result==null")
    public void updateUserInfo(UserVo vo) {

        userMapper.updateUserInfo(vo);
    }

    @Override
//    @Cacheable(cacheNames = "getUserInfo",key = "#vo.id",unless = "#result==null")
    public UserVo getUserInfo(UserVo vo ) {
        return userMapper.getUserInfoByCondition(vo);
    }

    @Override
    public void updatePass(UserVo vo) {
        userMapper.updatePass(vo);
    }
}
